package javafx.demo;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TestGrid extends Application {

	@Override
	public void start(Stage stage) throws Exception {
		VBox vbox = new VBox();
		vbox.setAlignment(Pos.BASELINE_CENTER);
		vbox.setSpacing(30);
		vbox.getChildren().add(getPassword());
		vbox.getChildren().add(getGrid());
		
		BorderPane outerRoot = new BorderPane();
		outerRoot.setCenter(vbox);

		Scene scene = new Scene(outerRoot, 600, 500);

		stage.setTitle("Hello World!");
		stage.setScene(scene);
		stage.show();

	}

	private GridPane getGrid(){
		GridPane grid = new GridPane();
		grid.setPadding(new Insets(10, 10, 10, 10));
		grid.setVgap(5);
		grid.setHgap(5);
		//Defining the Name text field
		final TextField name = new TextField();
		name.setPromptText("Enter your first name.");
		name.setPrefColumnCount(10);
		name.getText();
		GridPane.setConstraints(name, 0, 0);
		grid.getChildren().add(name);
		//Defining the Last Name text field
		final TextField lastName = new TextField();
		lastName.setPromptText("Enter your last name.");
		GridPane.setConstraints(lastName, 0, 1);
		grid.getChildren().add(lastName);
		//Defining the Comment text field
		final TextField comment = new TextField();
		comment.setPrefColumnCount(15);
		comment.setPromptText("Enter your comment.");
		GridPane.setConstraints(comment, 0, 2);
		grid.getChildren().add(comment);
		//Defining the Submit button
		Button submit = new Button("Submit");
		GridPane.setConstraints(submit, 1, 0);
		grid.getChildren().add(submit);
		//Defining the Clear button
		Button clear = new Button("Clear");
		GridPane.setConstraints(clear, 1, 1);
		grid.getChildren().add(clear);
		
		final Label label = new Label();
		GridPane.setConstraints(label, 0, 3);
		GridPane.setColumnSpan(label, 2);
		grid.getChildren().add(label);

		//Setting an action for the Submit button
		submit.setOnAction(new EventHandler<ActionEvent>() {

		@Override
		    public void handle(ActionEvent e) {
		        if ((comment.getText() != null && !comment.getText().isEmpty())) {
		            label.setText(name.getText() + " " + lastName.getText() + ", "
		                + "thank you for your comment!");
		        } else {
		            label.setText("You have not left a comment.");
		        }
		     }
		 });
		 
		//Setting an action for the Clear button
		clear.setOnAction(new EventHandler<ActionEvent>() {

		@Override
		    public void handle(ActionEvent e) {
		        name.setText("");
		        lastName.setText("");
		        comment.setText("");
		        label.setText(null);
		    }
		}); 
		return grid;
	}
	
	private VBox getPassword() {
		final Label message = new Label("");

		VBox vb = new VBox();
		vb.setPadding(new Insets(10, 0, 0, 10));
		vb.setSpacing(10);
		HBox hb = new HBox();
		hb.setSpacing(10);
		hb.setAlignment(Pos.CENTER_LEFT);

		Label label = new Label("Password");
		final PasswordField pb = new PasswordField();

		pb.setOnAction(new EventHandler<ActionEvent>() {
		    @Override public void handle(ActionEvent e) {
		        if (!pb.getText().equals("T2f$Ay!")) {
		            message.setText("Your password is incorrect!");
		            message.setTextFill(Color.rgb(210, 39, 30));
		        } else {
		            message.setText("Your password has been confirmed");
		            message.setTextFill(Color.rgb(21, 117, 84));
		        }
		        pb.setText("");
		    }
		});

		hb.getChildren().addAll(label, pb);
		vb.getChildren().addAll(hb, message);
		return vb;
	}
	public static void main(String[] args) {
		launch(args);
	}
}
